package com.baixun.gassystem.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baixun.gasentity.entity.SysLawEnforcementDevice;
import com.baixun.gasentity.vo.LawEnforcementDeviceExportVO;
import com.baixun.gassystem.mapper.SysLawEnforcementDeviceMapper;
import com.baixun.gassystem.service.SysLawEnforcementDeviceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 执法仪操作列表的服务实现类
 * 实现分页查询和不分页查询功能，支持根据场站名称和报警类型进行筛选
 */
@Service
public class SysLawEnforcementDeviceServiceImpl implements SysLawEnforcementDeviceService {

    @Autowired
    private SysLawEnforcementDeviceMapper sysLawEnforcementDeviceMapper;

    /**
     * 分页查询执法仪记录
     */
    @Override
    public IPage<SysLawEnforcementDevice> selectByStationAndTypeWithPagination(Page<SysLawEnforcementDevice> page,
                                                                               String stationName,
                                                                               String alarmType) {
        return sysLawEnforcementDeviceMapper.selectByStationAndTypeWithPagination(page, stationName, alarmType);
    }

    /**
     * 不分页查询执法仪记录
     */
    @Override
    public List<SysLawEnforcementDevice> selectByStationAndType(String stationName, String alarmType) {
        return sysLawEnforcementDeviceMapper.selectByStationAndType(stationName, alarmType);
    }


    /**
     * 导出执法仪记录为 Excel
     * @param stationName 场站名称
     * @param alarmType 报警类型
     * @param outputStream 输出流，导出到文件时使用
     */
    @Override
    public void exportLawEnforcementDeviceToExcel(String stationName, String alarmType, ServletOutputStream outputStream) {
        // 查询数据
        List<SysLawEnforcementDevice> lawEnforcementDevices = selectByStationAndType(stationName, alarmType);

        // 如果数据为空，直接返回
        if (CollectionUtils.isEmpty(lawEnforcementDevices)) {
            throw new RuntimeException("没有找到符合条件的执法仪操作记录");
        }

        // 将查询的数据转换为导出对象
        List<LawEnforcementDeviceExportVO> exportData = lawEnforcementDevices.stream().map(lawDevice -> {
            LawEnforcementDeviceExportVO exportVO = new LawEnforcementDeviceExportVO();
            exportVO.setLawDeviceId(lawDevice.getLawDeviceId());
            exportVO.setImageUrl(lawDevice.getImageUrl());
            exportVO.setStationName(lawDevice.getStationName());
            exportVO.setAlarmType(lawDevice.getAlarmType());
            exportVO.setCreateTime(lawDevice.getCreateTime().toString()); // 转为字符串格式
            return exportVO;
        }).collect(Collectors.toList());

        // 使用 EasyExcel 导出数据到 Excel
        EasyExcel.write(outputStream, LawEnforcementDeviceExportVO.class).sheet("执法仪记录").doWrite(exportData);
    }
 }

